package com.hellobike.haxing.modules.product.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hellobike.haxing.modules.product.entity.Promotion;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * @Description: 活动主信息
 * @Author: haxing-boot
 * @Date: 2020-01-23
 * @Version: V1.0
 */
public interface PromotionMapper extends BaseMapper<Promotion> {

    @Select("SELECT id, name, type, begin_time, end_time FROM t_promotion WHERE deleted=0 AND end_time > now() AND status=1 \n" +
            "\tAND EXISTS (SELECT 1 FROM t_promotion_group WHERE promotion_id=t_promotion.id AND product_id=#{productId} AND status=1 AND deleted=0)\n" +
            "UNION\n" +
            "SELECT id, name, type, begin_time, end_time FROM t_promotion WHERE deleted=0 AND end_time > now() AND status=1 \n" +
            "\tAND EXISTS (SELECT 1 FROM t_promotion_flash WHERE promotion_id=t_promotion.id AND product_id=#{productId} AND status=1 AND deleted=0)")
    List<Promotion> getProductPromotions(@Param("productId") Long productId);

    @Select("\n" +
            "select count(1) from t_promotion \n" +
            "where id in \n" +
            "(select promotion_id from t_promotion_flash a where a.product_id=#{productId} and a.status=1 union " +
            "select promotion_id from t_promotion_group b where b.product_id=#{productId} and b.status=1)\n" +
            "and (begin_time < #{beginTime} or end_time > #{endTime}) and status=1 and id <> #{promotionId}")
    Integer check(@Param("promotionId") Long promotionId, @Param("productId") Long id, @Param("beginTime") Date beginTime,
                  @Param("endTime") Date endTime);
}
